--#include "data\config\activityconfig\BackStageActivity\DailyRechargeConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理
--#include "data\functions\Activity\BackStageActivity\BackStageActivity.lua"   once         -- 后台活动开启关闭管理

--每日充值开始
function StartDailyRecharge()
	local ssVar = System.getStaticVar()
	ssVar.dailyRechargeDay = 1
	ssVar.dailyRechargeEnd = nil
	gRankMgr_Clear(RANK_DEFINE_DAILY_RECHARGE)
	OpenBackStageActivity(CommonActivity_DailyRecharge)
end

--每日充值结束
function EndDailyRecharge()
	local ssVar = System.getStaticVar()
	--ssVar.dailyRechargeDay = nil
	ssVar.dailyRechargeEnd = 1
	CloseBackStageActivity(CommonActivity_DailyRecharge)
end

--跨天发送邮件
function DailyRechargeSendMailTotal()
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		return
	end
	local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_DAILY_RECHARGE].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local actorid = tonumber(Ranking.getId(item))
			local subIndex = ssVar.dailyRechargeDay
			local data = Ranking.getSub(item, subIndex)
			local dataTable
			if not data or data == "" then
				dataTable = {}
			else
				dataTable = BaseFuc_unserialize(data)
			end
			DailyRechargeSendMail(actorid, dataTable)
		end
	end
end


--服务器跨天
function DailyRechargeCrossDay()
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		return
	end

	DailyRechargeSendMailTotal()

	if not System.IsCommonActivityOpen(CommonActivity_DailyRecharge) then
		ssVar.dailyRechargeDay = nil
	else
		ssVar.dailyRechargeDay = ssVar.dailyRechargeDay + 1

		local players = LuaHelp.getAllActorList()
		if players then	
			for k, v in ipairs(players) do	
				OpenBackStageActivity(CommonActivity_DailyRecharge, v)
			end
		end	
	end
end

--每日充值元宝
function DailyRechargeWithDrawYuanBao(sysarg, Gold)
	if not System.IsCommonActivityOpen(CommonActivity_DailyRecharge) then
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		return
	end
	if ssVar.dailyRechargeEnd and ssVar.dailyRechargeEnd == 1 then
		return 
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_DailyRecharge,0,0,0,0)
	local cfg = DailyRechargeCfg[nPlanId] 
	if not cfg then
		cfg = DailyRechargeCfg[#DailyRechargeCfg]
	end

	local itemIndex = ssVar.dailyRechargeDay
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex)

	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end
	for idx = 1, #cfg.Gold do
		if myData[idx] and myData[idx] > 1 then
		elseif myData[idx] and myData[idx] == 1 then
			break
		else
			if cfg.Gold[idx] <= Gold then
				myData[idx] = 1
				local newData = BaseFuc_serialize(myData)
				gRankMgr_SetRankEx(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex, newData)
				SendDailyRechargeData(sysarg, nil, myData)
			end
			break
		end
	end
end

--跨天邮件玩家
function DailyRechargeSendMail(actorId, dataTable)
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		return
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_DailyRecharge,0,0,0,0)
	local cfg = DailyRechargeCfg[nPlanId] 
	if not cfg then
		cfg = DailyRechargeCfg[#DailyRechargeCfg]
	end
	if not dataTable then
		return
	end

	local day = ssVar.dailyRechargeDay
	local rewardCfg = cfg.Rewards[day]
	if not rewardCfg then
		rewardCfg = cfg.Rewards[#cfg.Rewards]	
	end

	for idx = 1, #cfg.Gold do
		if dataTable[idx] and dataTable[idx] == 1 then
			local rewards = rewardCfg[idx]
			local title = Lang.ScriptTips.DailyRecharge001
			local content = Lang.ScriptTips.DailyRecharge002
			Awards.GiveToMailById(actorId, rewards, title, content, LogIds.DailyRechargeLogId, Lang.LogTips.DailyRecharge)
			break
		end
	end
end

--发送每日充值数据
function SendDailyRechargeData(sysarg, packet, myData)
	if not System.IsCommonActivityOpen(CommonActivity_DailyRecharge) then
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		return
	end
	if ssVar.dailyRechargeEnd and ssVar.dailyRechargeEnd == 1 then
		return 
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_DailyRecharge,0,0,0,0)
	if nPlanId == 0 or nPlanId > #DailyRechargeCfg then
		nPlanId = #DailyRechargeCfg
	end
	local cfg = DailyRechargeCfg[nPlanId] 

	if not myData then
		local itemIndex = ssVar.dailyRechargeDay
		local actorid = Actor.getActorId(sysarg)
		local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex)
		myData = BaseFuc_unserialize(data)
	end

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end


	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetDailyRechargeInfo)
	if (pack == nil) then
		return
	end
	DataPack.writeByte(pack, nPlanId)
	local LeftTime = System.GetCommonActivityLeftTime(CommonActivity_DailyRecharge)
	DataPack.writeUInt(pack, LeftTime or 0)
	DataPack.writeByte(pack, ssVar.dailyRechargeDay or 0)
	local len = #cfg.Gold
	for idx = 1, len do
		local state = myData[idx] or 0
		if state < 2 or (state == 2 and idx == len) then
			DataPack.writeByte(pack, idx)
			DataPack.writeByte(pack, state)
			break
		end
	end
	DataPack.flush(pack)
end

--领取奖励
function GetDailyRechargeAwards(sysarg, packet)
	local Index = DataPack.readByte(packet)
	if not System.IsCommonActivityOpen(CommonActivity_DailyRecharge) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge003, ttFlyTip)
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.dailyRechargeDay then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge003, ttFlyTip)
		return
	end
	if ssVar.dailyRechargeEnd and ssVar.dailyRechargeEnd == 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge004, ttFlyTip)
		return 
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_DailyRecharge,0,0,0,0)
	local cfg = DailyRechargeCfg[nPlanId] 
	if not cfg then
		cfg = DailyRechargeCfg[#DailyRechargeCfg]
	end
	if Index > #cfg.Gold then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge005, ttFlyTip)
		return
	end

	local itemIndex = ssVar.dailyRechargeDay 
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge006, ttFlyTip)
		return
	end
	if not myData[Index] or myData[Index] ~= 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DailyRecharge006, ttFlyTip)
		return
	end

	local day = ssVar.dailyRechargeDay

	local rewardCfg = cfg.Rewards[day]
	if not rewardCfg then
		rewardCfg = cfg.Rewards[#cfg.Rewards]	
	end

	local rewards = rewardCfg[Index]

	myData[Index] = 2
	
	if IsBagGridEnough(sysarg, rewards) then
		GiveCommonAward(sysarg, rewards, LogIds.DailyRechargeLogId, Lang.LogTips.DailyRecharge)
	else
		local actorId = Actor.getActorId(sysarg)
		local title = Lang.ScriptTips.DailyRecharge001
		local content = Lang.ScriptTips.DailyRecharge002
		Awards.GiveToMailById(actorId, rewards, title, content, LogIds.DailyRechargeLogId, Lang.LogTips.DailyRecharge)
	end

	local newData = BaseFuc_serialize(myData)
	gRankMgr_SetRankEx(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex, newData)

	if Index == #cfg.Gold then
		CloseBackStageActivity(CommonActivity_DailyRecharge, sysarg)
	else
		SendDailyRechargeData(sysarg, nil, myData)
	end
end

--判断每日充值是否全部完成
function GetDailyRechargeState(sysarg)
	local ssVar = System.getStaticVar()
	if not System.IsCommonActivityOpen(CommonActivity_DailyRecharge) then
		--兼容处理 在需要的时候开启
		--[[
        if ssVar.dailyRechargeDay >= 1 and ssVar.dailyRechargeEnd == nil then
            ssVar.dailyRechargeEnd = 1
            DailyRechargeCrossDay()
        end
        ]]
		return true
	end
	
	if not ssVar.dailyRechargeDay then
		return true
	end
	if ssVar.dailyRechargeEnd and ssVar.dailyRechargeEnd == 1 then
		return true
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_DailyRecharge,0,0,0,0)
	if nPlanId == 0 or nPlanId > #DailyRechargeCfg then
		nPlanId = #DailyRechargeCfg
	end
	local cfg = DailyRechargeCfg[nPlanId] 

	local itemIndex = ssVar.dailyRechargeDay

	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_DAILY_RECHARGE, itemIndex)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local len = #cfg.Gold
	if myData[len] and myData[len] == 2 then
		return true
	end 
	return false
end